package com.kidedu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

import com.kidedu.entity.KArt;
import com.kidedu.entity.KStudent;
import com.kidedu.entity.dto.KArtDTO;
import com.kidedu.entity.dto.KGASDTO;
import com.kidedu.service.IKArtService;
import com.kidedu.service.IKGradeService;
import com.kidedu.service.IKStudentService;
import com.kidedu.util.ArtVoStudent;
import com.kidedu.util.BaseAction;
import com.kidedu.util.FileUtil;
import com.kidedu.util.Pagination;
import com.kidedu.util.RespJSON;
import com.kidedu.util.SpringContextUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class KArtAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private String startTimeS;
	
	private String endTimeS;
	
	private String keyword;
	
	private Byte A_id;
	
	private String comment;
	
	private Byte A_type;
	
	private String t_id;
	
	private String work;
	
	private String student;
	
	private String grade;
	
	private File upload;
	
	private String filename;
	
	private int pageNo;
	
	private String basePath = "D:/cct/apache-tomcat-7.0.68/webapps/KidEdu/blog/";
	
	private IKArtService iKArtService = (IKArtService) SpringContextUtil.getBean("iKArtService");
	private IKStudentService iKStudentService = (IKStudentService) SpringContextUtil.getBean("iKStudentService");
	private IKGradeService ikGradeService = (IKGradeService) SpringContextUtil.getBean("iKGradeService");
	private Pagination<KArtDTO> kArtPagList;
	private KGASDTO kGASList;
	
	private JSONObject json=new JSONObject();
	
	
	public String   viewDetailArt()
	{
		ActionContext.getContext().getSession().put("aid",A_id);
		return Action.SUCCESS;
	}
	public String selectKArtListByDate(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
		Date startTime;
		Date endTime;
		try {
			startTime = sdf.parse(startTimeS);
			endTime = sdf.parse(endTimeS);
			kArtPagList = iKArtService.selectKArtListByDate(startTime, endTime, keyword, pageNo);
		} catch (ParseException e) {
			e.printStackTrace();
		}	
		return "JSON";
	}

	/*班主任查看作品*/
	public String selectKArtListByTea(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
		Date startTime;
		Date endTime;
		try {
			startTime = sdf.parse(startTimeS);
			endTime = sdf.parse(endTimeS);
			kArtPagList = iKArtService.selectKArtListByTea(startTime, endTime, keyword, pageNo, t_id);
			kGASList = ikGradeService.getGASbyTea(t_id);
		} catch (ParseException e) {
			e.printStackTrace();
		}	
		return "JSON";
	}
	
	/*修改优秀作品*/
	public String updateKArt(){
		iKArtService.updateKArt(A_id, comment, A_type);
		return "JSON";
	}
	
	/*删除作品*/
	public String deleteKArt(){
		iKArtService.deleteKArt(A_id);
		return "JSON";
	}
	
	/*上传作品*/
	public String addKArt(){
		if(filename.equals("0")){
			filename = "";
		}
		iKArtService.addKArt(work, filename, student, grade, comment, A_type, t_id);
		return "JSON";
	}
	
	/*上传文件*/
	public String upload() throws FileNotFoundException{
		FileInputStream in = new FileInputStream(upload);
		FileUtil fileUtil = new FileUtil();
		fileUtil.saveFile(in, basePath, filename);
		return "JSON";
	}
	
	public String Stu_artDetail()
	{
		RespJSON respJSON = new RespJSON();
		KArt art=iKArtService.ArtDetail(A_id);
		String Stu_id=art.getAstudentid();
	    KStudent student=	iKStudentService.selectSingleStudent(Stu_id);
		ArtVoStudent avs=new ArtVoStudent();
		avs.setArt(art);
		avs.setStudent(student);
	    respJSON.setStatusCode(200);
		respJSON.setStatusText("OK");
		respJSON.setData(avs);
		json = JSONObject.fromObject(respJSON);
		return Action.SUCCESS;
	}
	public String getStartTimeS() {
		return startTimeS;
	}

	public void setStartTimeS(String startTimeS) {
		this.startTimeS = startTimeS;
	}

	public String getEndTimeS() {
		return endTimeS;
	}

	public void setEndTimeS(String endTimeS) {
		this.endTimeS = endTimeS;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	public Pagination<KArtDTO> getkArtPagList() {
		return kArtPagList;
	}

	public void setkArtPagList(Pagination<KArtDTO> kArtPagList) {
		this.kArtPagList = kArtPagList;
	}

	public JSONObject getJson() {
		return json;
	}

	public void setJson(JSONObject json) {
		this.json = json;
	}

	public Byte getA_id() {
		return A_id;
	}

	public void setA_id(Byte a_id) {
		A_id = a_id;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Byte getA_type() {
		return A_type;
	}

	public void setA_type(Byte a_type) {
		A_type = a_type;
	}

	public String getT_id() {
		return t_id;
	}

	public void setT_id(String t_id) {
		this.t_id = t_id;
	}

	public KGASDTO getkGASList() {
		return kGASList;
	}

	public void setkGASList(KGASDTO kGASList) {
		this.kGASList = kGASList;
	}

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = work;
	}

	public String getStudent() {
		return student;
	}

	public void setStudent(String student) {
		this.student = student;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	
}
